import {_decorator, Component, Node, Sprite} from 'cc';
import {ShopItemController} from "db://assets/scripts/ui/shopPanel/ShopItemController";
import {AudioManager} from "db://assets/scripts/common/AudioManager";
import {Utils} from "db://assets/scripts/utils/Utils";
import {CSVManager} from "db://assets/scripts/common/CSVManager";
import {UIManager} from "db://assets/scripts/common/UIManager";
import {PlayerDataManager} from "db://assets/scripts/common/PlayerDataManager";
import {SkillConverter} from "db://assets/scripts/data/converter/SkillConverter";
import {Skill} from "db://assets/scripts/data/model/Skill";
import {AudioPaths} from "db://assets/scripts/common/AudioPaths";
import {PlayerSkillManager} from "db://assets/scripts/game/role/player/PlayerSkillManager";

const {ccclass, property} = _decorator;

/**
 * 商店控制器
 */
@ccclass('ShopPanelController')
export class ShopPanelController extends Component {
    /**
     * 商品列表
     */
    @property(Node)
    public skillsNode: Node;
    /**
     * 刷新按钮
     */
    @property(Sprite)
    public refreshIconSprite: Sprite;

    /**
     * 回调
     */
    private callback: Function;

    /**
     * 初始化商店
     *
     * @param callback 回调
     */
    public init(callback: Function) {
        this.callback = callback;
    }

    /**
     * 展示商店
     */
    public show() {
        // 获取玩家未解锁技能
        let lockSkillIds: any = PlayerSkillManager.getInstance().getLockPlayerSkill();
        // 打乱
        lockSkillIds = Utils.shuffle(lockSkillIds);

        // 获取所有技能
        let playerSkillCSV = CSVManager.getInstance().getTable("playerSkill");
        // 类型转换
        let playerSkills = SkillConverter.convertFromCSV(playerSkillCSV);

        // 配置技能
        this.skillsNode.children.forEach((child: Node, idx: number) => {
            if (lockSkillIds[idx]) {
                let skill: Skill = playerSkills.find(value => value === lockSkillIds[idx]);
                let shopItemController = child.getComponent(ShopItemController);
                shopItemController.init(skill, () => {
                    this.close();
                });
                // 激活节点
                child.active = true;
            } else {
                // 冻结节点
                child.active = false;
            }
        })
    }

    /**
     * 放弃购买
     */
    public onCancel() {
        AudioManager.getInstance().playSound(AudioPaths.clickSound);
        this.close();
    }

    /**
     * 刷新商店
     */
    public onRefresh() {
        AudioManager.getInstance().playSound(AudioPaths.clickSound);
        this.show();
    }

    /**
     * 关闭事件
     */
    public onClose() {
        AudioManager.getInstance().playSound(AudioPaths.clickSound);
        this.close();
    }

    /**
     * 关闭
     */
    private close() {
        // 隐藏店铺界面
        UIManager.getInstance().hideDialog("shop/shopPanel");
        // 显示战斗界面
        UIManager.getInstance().showDialog("fight/fightPanel");
        // 回调
        this.callback && this.callback();
    }
}